public class Demo4 {
	private static boolean flg=false;
	/**
	 * 中断线程并不是直接让他结束，而是让他尽快结束
	 * @param args
	 */
	public static void main1(String[] args) throws InterruptedException {
		Thread t1=new Thread(()->{
			while(!flg) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				System.out.println("hello thread");
			}
		});
		t1.start();
		for(int i =0;i<5;i++) {
			Thread.sleep(1000);
			System.out.println("hello main");
		}
		flg=true;//手动来打一个标志位来进行中断线程
	}

	public static void main(String[] args) throws InterruptedException {
		Thread t1=new Thread(()->{
			while(!Thread.currentThread().isInterrupted()) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				//1.假装没听见，继续执行
				//2.break立即结束这个线程
				System.out.println("hello thread");
				break;
				//这种情况就是让t这个线程执行完了，在再进行终止
				//interrupt会提前唤醒sleep，会清空标志位
			}
		});
		t1.start();
		Thread.sleep(5000);
//		for(int i=0;i<5;i++) {
//			System.out.println("hello main");
//		}
		System.out.println("让线程t终止");
		t1.interrupt();
	}
}
